package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ORM_O01;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.ORC;
import ca.uhn.hl7v2.model.v24.segment.PID;
import ca.uhn.hl7v2.model.v24.segment.QRD;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luoxiaoming on 18-10-16 上午10:30
 * @email luoxiaoming@herench.com
 */
public class OrdersConvert extends AbstractMessageConvert {

    @Override
    public Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> params = new HashMap<>();
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        String patientId = qrd.getQrd4_QueryID().getValue();
        String startTime = qrd.getQrd1_QueryDateTime().getTs1_TimeOfAnEvent().getValue();
        String endTime = qrd.getQrd2_QueryFormatCode().getValue();
        params.put("patientId", patientId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return params;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>>) message;
        ORM_O01 ormO01 = new ORM_O01();
        ormO01.initQuickstart("ORM_O01", "ORM_O01", "A");
        MSH msh = ormO01.getMSH();
        initResponseMessageMSH(msh);
        if(res.size()>0) {
            Map<String, Object> reqMap = res.get(0);

            PID pid = ormO01.getPATIENT().getPID();

            pid.getPid2_PatientID().getCx1_ID().setValue(String.valueOf(reqMap.get("patientId")));
            pid.getPid2_PatientID().getCx2_CheckDigit().setValue(String.valueOf(reqMap.get("visitId")));
            pid.getPid2_PatientID().getCx7_EffectiveDate().setValue(String.valueOf(reqMap.get("startDateTime")));


            for (int i = 0; i < res.size(); i++) {
                ORC orc = ormO01.getORDER(i).getORC();
                Map<String, Object> resMap = res.get(i);
                orc.getOrc1_OrderControl().setValue(String.valueOf(resMap.get("orderCode")));
                orc.getOrc2_PlacerOrderNumber().getEi1_EntityIdentifier().setValue(String.valueOf(resMap.get("orderNo")));
                orc.getOrc2_PlacerOrderNumber().getEi2_NamespaceID().setValue(String.valueOf(resMap.get("orderSubNo")));
                orc.getOrc2_PlacerOrderNumber().getEi3_UniversalID().setValue(String.valueOf(resMap.get("orderText")));
                orc.getOrc2_PlacerOrderNumber().getEi4_UniversalIDType().setValue(String.valueOf(resMap.get("dosage")));
                orc.getOrc3_FillerOrderNumber().getEi1_EntityIdentifier().setValue(String.valueOf(resMap.get("dosageUnits")));
                orc.getOrc3_FillerOrderNumber().getEi2_NamespaceID().setValue(String.valueOf(resMap.get("administration")));
                orc.getOrc3_FillerOrderNumber().getEi3_UniversalID().setValue(String.valueOf(resMap.get("duration")));
                orc.getOrc3_FillerOrderNumber().getEi4_UniversalIDType().setValue(String.valueOf(resMap.get("durationUnits")));
                orc.getOrc4_PlacerGroupNumber().getEi1_EntityIdentifier().setValue(String.valueOf(resMap.get("frequency")));
                orc.getOrc4_PlacerGroupNumber().getEi2_NamespaceID().setValue(String.valueOf(resMap.get("orderSpec")));
                orc.getOrc4_PlacerGroupNumber().getEi3_UniversalID().setValue(String.valueOf(resMap.get("orderClass")));
                orc.getOrc4_PlacerGroupNumber().getEi4_UniversalIDType().setValue(String.valueOf(resMap.get("stopDateTime")));
                orc.getOrc5_OrderStatus().setValue(String.valueOf(resMap.get("orderStatus")));
                orc.getOrc6_ResponseFlag().setValue(String.valueOf(resMap.get("repeatIndicator")));
                orc.getOrc10_EnteredBy(0).getXcn1_IDNumber().setValue(String.valueOf(resMap.get("orderingDept")));
                orc.getOrc10_EnteredBy(0).getXcn2_FamilyName().getFn1_Surname().setValue(String.valueOf(resMap.get("doctor")));
                orc.getOrc10_EnteredBy(0).getXcn3_GivenName().setValue(String.valueOf(resMap.get("nurse")));
                orc.getOrc12_OrderingProvider(0).getXcn1_IDNumber().setValue(String.valueOf(resMap.get("freqDetail")));
                orc.getOrc12_OrderingProvider(0).getXcn3_GivenName().setValue(String.valueOf(resMap.get("stopDoctor")));
                orc.getOrc12_OrderingProvider(0).getXcn5_SuffixEgJRorIII().setValue(String.valueOf(resMap.get("orderInsuranceType")));
                orc.getOrc12_OrderingProvider(0).getXcn6_PrefixEgDR().setValue(String.valueOf(resMap.get("localRecordPk")));
                orc.getOrc15_OrderEffectiveDateTime().getTs1_TimeOfAnEvent().setValue(String.valueOf(resMap.get("enterDateTime")));
                orc.getOrc15_OrderEffectiveDateTime().getTs2_DegreeOfPrecision().setValue(String.valueOf(resMap.get("lastModifyDate")));
            }
        }else {
            msh.getMsh15_AcceptAcknowledgmentType().setValue("无数据");
        }
        return ormO01;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_Q01 qry = new QRY_Q01();
        qry.initQuickstart("QRY_Q01", "Q01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("300040");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("PACS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        QRD qrd = qry.getQRD();
        addQrd(qrd, "testNo", "M100610000001E");
        addQrd(qrd, "resultStatus", "2");
        addQrd(qrd, "reporter", "1503");
        addQrd(qrd, "verifiedBy", "1503");
        return qry;
    }
}
